package com.anp.business.utils;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateFormatters {

    public static final String ISO_DATE_FORMAT = "yyyyMMdd";
    public static final String ISO_TIME_FORMAT = "HHmmss";
    public static final String ISO_DATE_TIME_FORMAT = "yyyyMMddHHmmss";

    public static final String ISO_DATE_TIME_SSS_FORMAT = "yyyyMMddHHmmssSSS";
    public static final String ISO_TIME_SSS_FORMAT = "HHmmssSSS";

    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String TIME_FORMAT = "HH:mm:ss";


    public static Date parse(String date, String pattern) throws ParseException {
        return DateUtils.parseDate(date, pattern);
    }

    public static String format(Date date, String pattern) {
        if (null == date) {
            return DateFormatUtils.format(new Date(), pattern, TimeZone.getDefault());
        }
        return DateFormatUtils.format(date, pattern, TimeZone.getDefault());
    }

    public static String format(String pattern) {
        return format(new Date(), pattern);
    }

    public static String getWeekDay(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /**
     * 计算天
     *
     * @param date
     * @param day
     * @return
     */
    public static Date calcuDate(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);
        return calendar.getTime();
    }
}
